package com.doradance.app.home;


import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DrawableUtils;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.ParamKey;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.XCRoundRectImageView;
import com.doradance.app.activity.ActivityDetailActivity;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.FreeItemEntity;
import com.doradance.entity.HotActivityEntity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class HotActivityFragment extends SwipeListFragment {
    HotActivityAdapter hotActivityAdapter;
    private int mPage = 0;

    public HotActivityFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {

        hotActivityAdapter = new HotActivityAdapter(getActivity());
        return hotActivityAdapter;
    }

    private ListDataRequestListener<HotActivityEntity> mResponceListener = new ListDataRequestListener<>(this, HotActivityEntity.class, false);

    @Override
    public void onStartLoading() {
        if (mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
        }
        MyHttpRequest request = new MyHttpRequest(getContext());
        UrlParams urlParams = new UrlParams();
        urlParams.put(ParamKey.PAGE, mPage + "");
        urlParams.put(ParamKey.SIZE, 8 + "");
        request.get(UrlCenter.ACTIVIES, urlParams, mResponceListener);
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 1;
        startLoading();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long itemId) {
        super.onItemClick(arg0, arg1, position, itemId);
        HotActivityEntity o = (HotActivityEntity) getAdapter().getData().get(position);
        ActivityDetailActivity.start(getActivity(),o.getId());
    }

    public class HotActivityAdapter extends RecyclerViewBaseAdapter<HotActivityEntity> {
        public HotActivityAdapter(Context con) {
            super(con);
        }
        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.hot_activity_item));
        }



        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int position) {
            VH holder = (VH) viewHolder;
            HotActivityEntity hotActivityEntity = getData().get(position);
            Glide.with(getContext()).load(hotActivityEntity.getCover()).asBitmap().into(holder.mIvHotActivity);
            holder.mTvHotTitle.setText(hotActivityEntity.getName());
            holder.mTvConduct.setText(hotActivityEntity.getStatus_name());
            holder.mTvTime.setText(hotActivityEntity.getTime());
            holder.mTvPeople.setText(hotActivityEntity.getMember_count()+"/"+hotActivityEntity.getMember_limit());
            holder.mTvFree.setText(hotActivityEntity.getPrice_text());
            if(hotActivityEntity.isSign_up()==true){
                holder.mTvSignNum.setText(hotActivityEntity.getSign_up_text());
                holder.mTvSignNum.setTextColor(Color.parseColor("#FEC86B"));
                holder.mTvSignNum.setBackgroundResource(R.drawable.hot_num_bg);
            }else{
                holder.mTvSignNum.setText("报名已截止");
                holder.mTvSignNum.setTextColor(Color.parseColor("#ffffff"));
                holder.mTvSignNum.setBackgroundResource(R.drawable.hot_avtivity_over);
            }

        }

        public class VH extends RecyclerView.ViewHolder {
            @Bind(R.id.iv_hot_activity)
            XCRoundRectImageView mIvHotActivity;
            @Bind(R.id.tv_hot_title)
            TextView mTvHotTitle;
            @Bind(R.id.tv_conduct)
            TextView mTvConduct;
            @Bind(R.id.tv_time)
            TextView mTvTime;
            @Bind(R.id.tv_people)
            TextView mTvPeople;
            @Bind(R.id.tv_free)
            TextView mTvFree;
            @Bind(R.id.tv_sign_num)
            TextView mTvSignNum;

            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

}
